package persentation.presentUniversityDean;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JButton;

import businesslogic.chooseCourseBl.ChooseCourseController;
import businesslogic.informBl.InformController;
import businesslogic.userBl.UniversityDeanController;
import vo.chooseCourseVO.ChooseCourseVO;
import vo.frameworkVO.FrameworkVO;
import vo.informVO.InformVO;
import vo.userVO.StudentVO;
import dataservice.ResultMessage;

import java.awt.Font;

public class ShenghexuankePanel extends JPanel {
	private JTable table;
	private MyTabbedPane tabbedPane;
	private JTable table_1;
	private InformVO informVO;
	private ChooseCourseVO chooseCourseVO;
	private UniversityDeanController uDeanController;
	private ChooseCourseController chooseCourseController;
	private InformController informController;
	private int times;
	private ArrayList<ChooseCourseVO> chooseCourseVOs;
	private ArrayList<ChooseCourseVO> chooseCourseVOsChecked;

	/**
	 * Create the panel.
	 */
	public ShenghexuankePanel(UniversityDeanController uDeanController) {
		this.uDeanController = uDeanController;
		informController = new InformController(uDeanController.getUserID());
		chooseCourseController = new ChooseCourseController(
				uDeanController.getUserID());
		setLayout(null);
		times = 1;
		try {
			System.out.println("***************************************");
			chooseCourseVOs = chooseCourseController.getAllStudentChoose();
			// System.out.println(chooseCourseVOs.get(0).getCourseName());
		} catch (Exception e1) {
			e1.printStackTrace();
			ExceptionHandler.networkException();
		}

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(22, 52, 470, 327);
		add(scrollPane);

		table = new JTable();
		table.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
			},
			new String[] {
				"\u8BFE\u7A0B\u53F7", "\u8BFE\u7A0B\u540D\u79F0", "\u5B66\u5206", "\u9009\u8BFE\u5B66\u751F\u5B66\u53F7", "\u9009\u8BFE\u5B66\u751F\u59D3\u540D"
			}
		) {
			boolean[] columnEditables = new boolean[] {
				false, false, false, false, false
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		table.getColumnModel().getColumn(0).setResizable(false);
		table.getColumnModel().getColumn(1).setResizable(false);
		table.getColumnModel().getColumn(2).setResizable(false);
		table.getColumnModel().getColumn(3).setResizable(false);
		table.getColumnModel().getColumn(4).setResizable(false);
		scrollPane.setViewportView(table);

		fillTable();

		/*
		 * JComboBox comboBox = new JComboBox(); comboBox.setBounds(135, 30,
		 * 104, 26); add(comboBox);
		 * 
		 * JLabel label = new JLabel("院系"); label.setBounds(68, 32, 67, 22);
		 * add(label);
		 * 
		 * JComboBox comboBox_1 = new JComboBox();
		 * comboBox_1.setToolTipText("请先选择院系"); comboBox_1.setBounds(318, 30,
		 * 104, 26); add(comboBox_1);
		 * 
		 * JLabel label_1 = new JLabel("课程"); label_1.setBounds(251, 32, 67,
		 * 22); add(label_1);
		 */

		JButton btnNewButton = new JButton("审核选课");
		btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// chooseCourseVOsChecked = ;
				try {
					System.out
							.println("***************************************");
					chooseCourseVOs = chooseCourseController
							.getAllStudentChoose();
					// System.out.println(chooseCourseVOs.get(0).getCourseName());
				} catch (Exception e1) {
					e1.printStackTrace();
					ExceptionHandler.networkException();
				}
				if (chooseCourseVOs.size() != 0) {
					try {
						chooseCourseVOsChecked = chooseCourseController
								.sortChooseCourse();
						System.out
								.print("***********************************************aaaaaaaaaaaaaa");
						//System.out.print(chooseCourseVOsChecked.get(0).getCourseName());
						fillTable_1();
						JOptionPane.showMessageDialog(null, "完成审核！",
								"南京大学选课系统", JOptionPane.INFORMATION_MESSAGE);
					} catch (Exception e1) {
						// TODO 自动生成的 catch 块
						e1.printStackTrace();
					}
				} else {
					JOptionPane.showMessageDialog(null, "没有要审核的新选课记录！",
							"南京大学选课系统", JOptionPane.INFORMATION_MESSAGE);
					
				}
			}
		});
		btnNewButton.setBounds(210, 428, 93, 35);
		add(btnNewButton);

		JButton button = new JButton("发送通知");
		button.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				int j = JOptionPane.showConfirmDialog(null, "确认发送通知？", "确认",
						JOptionPane.YES_NO_OPTION);
				if (j == JOptionPane.YES_OPTION) {
					if (chooseCourseVOsChecked != null && times > 0) {

						for (int i = 0; i < chooseCourseVOsChecked.size(); i++) {
							chooseCourseVO = chooseCourseVOsChecked.get(i);
							String message = chooseCourseVO.getStudentName()
									+ "你已选中《" + chooseCourseVO.getCourseName()
									+ "》";
							informVO = new InformVO(chooseCourseVO
									.getStudentId(), message);
							informController.sendStudentInform(informVO);

						}
						times--;
						JOptionPane.showMessageDialog(null, "发送通知成功！",
								"南京大学选课系统", JOptionPane.INFORMATION_MESSAGE);

					} else {
						JOptionPane.showMessageDialog(null, "您已发送过通知！",
								"南京大学选课系统", JOptionPane.INFORMATION_MESSAGE);
					}
				}
			}
		});
		button.setBounds(598, 428, 93, 35);
		add(button);

		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(526, 52, 242, 327);
		add(scrollPane_1);

		table_1 = new JTable();
		table_1.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		table_1.setModel(new DefaultTableModel(new Object[][] {
				{ null, null, null }, { null, null, null },
				{ null, null, null }, { null, null, null },
				{ null, null, null }, { null, null, null },
				{ null, null, null }, { null, null, null },
				{ null, null, null }, { null, null, null },
				{ null, null, null }, { null, null, null },
				{ null, null, null }, { null, null, null }, }, new String[] {
				"\u8BFE\u7A0B\u53F7", "\u8BFE\u7A0B\u540D",
				"\u5B66\u751F\u59D3\u540D" }) {
			boolean[] columnEditables = new boolean[] { false, false, false };

			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		table_1.getColumnModel().getColumn(0).setResizable(false);
		table_1.getColumnModel().getColumn(1).setResizable(false);
		table_1.getColumnModel().getColumn(2).setResizable(false);
		scrollPane_1.setViewportView(table_1);

		JLabel label = new JLabel("待审核的选课记录");
		label.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		label.setBounds(204, 25, 131, 16);
		add(label);

		JLabel label_1 = new JLabel("审核结果");
		label_1.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		label_1.setBounds(608, 25, 131, 16);
		add(label_1);
		// fillTable_1();
	}

	public void fillTable() {

		if (chooseCourseVOs.size() != 0) {
			DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
			tableModel.setRowCount(0);

			for (ChooseCourseVO chooseCourse : chooseCourseVOs) {
				String[] array = new String[5];
				array[0] = chooseCourse.getCourseId();
				array[1] = chooseCourse.getCourseName();
				array[2] = String.valueOf(chooseCourse.getCreidt());
				array[3] = chooseCourse.getStudentId();
				array[4] = chooseCourse.getStudentName();

				System.out.println(array[0]);
				System.out.println(array[1]);
				System.out.println(array[2]);
				System.out.println(array[3]);
				System.out.println(array[4]);
				tableModel.addRow(array);
				table.invalidate();
				table.updateUI();
			}
		} else {

		}

	}

	public void fillTable_1() {
		DefaultTableModel tableModel = (DefaultTableModel) table_1.getModel();
		tableModel.setRowCount(0);
		for (ChooseCourseVO chooseCourse : chooseCourseVOsChecked) {
			String[] array = new String[5];
			array[0] = chooseCourse.getCourseId();
			array[1] = chooseCourse.getCourseName();
			array[2] = chooseCourse.getStudentName();
			array[3] = chooseCourse.getStudentId();
			array[4] = String.valueOf(chooseCourse.getCreidt());

			System.out.println(array[0]);

			System.out.println(array[1]);
			System.out.println(array[2]);
			System.out.println(array[3]);
			System.out.println(array[4]);
			tableModel.addRow(array);
			table_1.invalidate();
			table_1.updateUI();
		}
	}

	public void setMyTabbedPane(MyTabbedPane tabbedPane) {
		this.tabbedPane = tabbedPane;
	}
}
